﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.BUSS.Settings;
using MpSoft.Library.DTO;

namespace MpSoft.Web.AdminCP.Controls.Configs
{
    public partial class ShippingMethodEdit : System.Web.UI.UserControl
    {
        #region Var

        private int shippingid = 0;

        #endregion

        #region Method

        private void BindControl()
        {
            if (shippingid > 0)
            {
                var obj = ShippingMethodBUSS.ShippingMethod_ByID(shippingid);
                if (obj != null)
                {
                    txtName.Text = obj.Name;
                    txtDescription.Text = obj.Description;
                    chkIsActive.Checked = obj.IsActive;
                    txtOrderSort.NumberIncremnet = obj.DisplayOrder;
                }
            }
        }

        private int Save()
        {
            ShippingMethod obj = null;
            if (shippingid > 0)
                obj = ShippingMethodBUSS.ShippingMethod_ByID(shippingid);
            if (obj == null)
                obj = new ShippingMethod();
            obj.Name = txtName.Text;
            obj.Description = txtDescription.Text;
            obj.IsActive = chkIsActive.Checked;
            obj.DisplayOrder = txtOrderSort.NumberIncremnet;
            if (shippingid > 0)
                return ShippingMethodBUSS.ShippingMethod_Update(obj);
            else
                return ShippingMethodBUSS.ShippingMethod_Add(obj);
        }

        private void Message(string message, int type)
        {
            switch (type)
            {
                case 1:
                    ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
                    if (shippingid < 1)
                        MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
                    break;
                case 2:
                    ltlMessage.Text = "<p class='message-warning'><i class='glyphicon-warning_sign'></i> " + message + "</p>";
                    break;
            }
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out shippingid);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Message("Cập nhật thành công!", 1);
        }

        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Response.Redirect("?mod=shippingmethod");
        }

        #endregion

        #region Properties

        public int ShippingMethodID { get { return shippingid; } }

        #endregion
    }
}